<template>
  <div class="app-container">
    <Find @findClicked="findData1" @Launchform="Launchform5" :OriginalFilter="{ static: true }" :formList="formList" @clickNumber1="clickNumber1" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <h1>单价作业</h1>
      <Find
        @Launchform="Launchform"
        :OriginalFilter="{ static: true }"
        :searchImg="false"
        style="margin-bottom: 10px"
        :formList="formList1"
        @findClicked="findData"
      />
      <h1>通用材料单价明细</h1>
      <Table
        @selectListShow="selectListShow"
        @selectList="selectList2"
        :isLoading="isLoading1"
        :selection="true"
        :tableHeader="tableHeader"
        :tableData="GeneralMaterialUnitPriceTableDate"
        :TableHeight="95"
      />
      <PageModule :PageData="{ buttonS: true, total: totalL }" :layout="'prev, pager'" @pageChange="pageChangeL" @pageSizeChange="pageSizeChangeL" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'GeneralMaterialUnitPriceFile'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { VendorQuery } from '@/api/WarehouseDepartment/FactoryStorage/FitOutsourcingProcessing'
import type { formListType } from '@/components/Find/index.vue'
import { ref, onMounted } from 'vue'
import {
  ChecktoCopyRecords,
  GeneralMaterialUnitPrice,
  FillInUnitPrice,
  UpdateMaterialFiling
} from '@/api/GeneralFinanceDepartment/FactoryMaterialCost/GeneralMaterialUnitPriceFile'
// 打勾记录复制
const ChecktoCopyRecordsParms = ref()

const selectListShow = (row?: any, visible?: boolean) => {
  if (visible) {
    row.vendorName = {
      selectValue: row.vendorName.selectValue,
      options: vendorListNo
    }
  } else {
    if (row !== undefined && row.vendorName === Object) {
      row.vendorName.options = []
    }
  }
}

const ChecktoCopyRecordsMethods = async () => {
  const result = await ChecktoCopyRecords(
    ChecktoCopyRecordsParms.value,
    FillInUnitPriceParms.value.map((item: any) => {
      item.vendorName = item.vendorName.selectValue
      return item
    })
  )
  if (result.code === 200) {
    GeneralMaterialUnitPriceMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const findData = (cont: string) => {
  if (cont === '填充厂商单价') {
    if (FillInUnitPriceVendorName.value === undefined) {
      JMessage.warning('厂商不可为空')
    } else if (FillInUnitPriceprive.value === '') {
      JMessage.warning('单价不可为空')
    } else if (FillInUnitPriceParms.value.length === 0) {
      JMessage.warning('请勾选正确的数据')
    } else {
      FillInUnitPriceMethods()
    }
  }
  if (cont === '打勾记录复制') {
    if (FillInUnitPriceParms.value.length === 0) {
      JMessage.warning('请勾选正确的数据')
    } else {
      ChecktoCopyRecordsParms.value = 1
      ChecktoCopyRecordsMethods()
    }
  }
  if (cont === '打勾记录删除') {
    if (FillInUnitPriceParms.value.length === 0) {
      JMessage.warning('请勾选正确的数据')
    } else {
      ChecktoCopyRecordsParms.value = 2
      handleCloseMateriaLKlc()
    }
  }
}

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确认要删除数据吗?')
    .then(async () => {
      ChecktoCopyRecordsMethods()
    })
    .catch(() => {})
}

// 更新材料建档
const UpdateMaterialFilingMethods = async () => {
  const result = await UpdateMaterialFiling(
    FillInUnitPriceParms.value.map((item: any) => {
      if (item.vendorName.selectValue !== undefined) {
        item.vendorName = item.vendorName.selectValue.split(' ')[1]
      }
      if (item.taxFlag) {
        item.taxFlag = 'Y'
      }

      return item
    })
  )
  if (result.code === 200) {
    GeneralMaterialUnitPriceMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const findData1 = (row: any) => {
  if (row === '更新材料建档') {
    if (FillInUnitPriceParms.value.length === 0) {
      JMessage.warning('请勾选正确的数据')
    } else {
      UpdateMaterialFilingMethods()
    }
  }
}

// 填充厂商单价
const FillInUnitPriceParms = ref([])

const FillInUnitPriceVendorName = ref('')

const FillInUnitPriceprive = ref('')

const FillInUnitPriceVendorNo = ref('')

const selectList2 = (row: any) => {
  FillInUnitPriceParms.value = row.map((item: any) => {
    return item
  })
}

const Launchform = (row: any) => {
  FillInUnitPriceVendorName.value = row[1].selectValue.split(' ')[1]
  FillInUnitPriceVendorNo.value = row[1].selectValue.split(' ')[0]
  FillInUnitPriceprive.value = row[0].inputValue
}

const FillInUnitPriceMethods = async () => {
  const result = await FillInUnitPrice(
    FillInUnitPriceVendorName.value,
    FillInUnitPriceVendorNo.value,
    FillInUnitPriceprive.value,
    FillInUnitPriceParms.value.map((item: any) => {
      item.vendorName = item.vendorName.selectValue
      return item
    })
  )
  if (result.code === 200) {
    GeneralMaterialUnitPriceMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 整体查询
const Launchform5 = (row: any) => {
  GeneralMaterialUnitPriceParms.value.vendorName = row[1].selectValue.split(' ')[0]
  GeneralMaterialUnitPriceParms.value.mtlName = row[2].inputValue
  GeneralMaterialUnitPriceParms.value.mtlColor = row[3].inputValue
  GeneralMaterialUnitPriceMethods()
}

// 通用材料单价明细查询
const GeneralMaterialUnitPriceParms = ref<any>({
  choose: '11',
  choose2: '',
  flag: '3',
  lotNo: '',
  mtlColor: '',
  mtlName: '',
  pageNum: 1,
  pageSize: 40,
  style: '',
  vendorName: ''
})

const GeneralMaterialUnitPriceTableDate = ref([])

const isLoading1 = ref(true)

const GeneralMaterialUnitPriceMethods = async () => {
  try {
    isLoading1.value = true
    const result = await GeneralMaterialUnitPrice(GeneralMaterialUnitPriceParms.value)
    GeneralMaterialUnitPriceTableDate.value = result.list.map((item: any) => {
      if (item.taxFlag === 'Y') {
        item.taxFlag = true
      }

      if (item.usage === null) {
        item.backgroundColor = '#FFE0E0'
      }

      item.vendorName = {
        selectValue: item.vendorName || '',
        options: []
      }

      item.change = 'Y'
      item.disableConfirm = true

      return item
    })
    totalL.value = result.total
    isLoading1.value = false
  } catch (error) {
    JMessage.error('查询出错')
    isLoading1.value = false
  }
}

const totalL = ref(0)

const pageChangeL = (val: any) => {
  GeneralMaterialUnitPriceParms.value.pageNum = val
  GeneralMaterialUnitPriceMethods()
}

const pageSizeChangeL = (val: any) => {
  GeneralMaterialUnitPriceParms.value.pageSize = val
  GeneralMaterialUnitPriceMethods()
}

// 厂商查询
const VendorQueryMethods = async () => {
  let NewArray = []
  const result = await VendorQuery()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])
  formList.value[1].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  formList1.value[1].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  vendorListNo.value = EvaluatesTheArrayReturnValue1(NewArray)
}

const vendorListNo = ref([])

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const clickNumber1 = (row: any) => {
  switch (row) {
    case '0': {
      console.log(row)
      GeneralMaterialUnitPriceParms.value.flag = 3
      break
    }
    case '1': {
      GeneralMaterialUnitPriceParms.value.flag = 2
      break
    }
    case '2': {
      GeneralMaterialUnitPriceParms.value.flag = 1
      break
    }
  }
  GeneralMaterialUnitPriceMethods()
}

onMounted(() => {
  VendorQueryMethods()

  GeneralMaterialUnitPriceMethods()
})

const formList = ref<any>([
  {
    type: 'button',
    checkboxLabel: '更新材料建档'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料颜色',
    inputValue: ''
  },
  {
    type: 'radio1',
    checkboxLabel: '有单价',
    checked: false,
    label: '1'
  },
  {
    type: 'radio1',
    checkboxLabel: '无单价',
    checked: false,
    label: '2'
  },
  {
    type: 'radio1',
    checkboxLabel: '全部',
    checked: true,
    label: '0'
  }
])

const formList1 = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '单价',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '填充厂商单价'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录复制'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录删除'
  }
])

// 表格数据
const tableHeader = ref([
  {
    label: '类型',
    prop: 'style',
    change: true
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    backgroundColor: true,
    change: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '是否含税',
    prop: 'taxFlag',
    type: 'checkbox'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '厂商',
    prop: 'vendorName',
    type: 'select'
  },
  {
    label: '生效日期',
    prop: 'cfmDate',
    type: 'dateSingle'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '操作员',
    prop: 'cfmUser',
    change: true
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 14px;
  margin-bottom: 5px;
}
</style>
